﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Bussiness;

namespace GUI
{
    public partial class frmSachMoi : Form
    {
        public frmSachMoi()
        {
            InitializeComponent();
        }

        private void lblSoLuongNhap_Click(object sender, EventArgs e)
        {

        }

        private void tbxSoLuongNhap_TextChanged(object sender, EventArgs e)
        {

        }

        private void cbxTheLoai_SelectedIndexChanged(object sender, EventArgs e) {
            //create ma the loai
            TheLoaiDTO theLoai = (TheLoaiDTO)cbxTheLoai.SelectedItem;
            SachService service = new SachService();

            tbxMaSach.Text = service.CreateMaSach(theLoai.MaTheLoai);
        }

        private void frmSachMoi_Load(object sender, EventArgs e) {
            //load danh sach the loai
            List<TheLoaiDTO> theLoaiList = new List<TheLoaiDTO>();
            TheLoaiService tlService = new TheLoaiService();
            theLoaiList = tlService.SelectAllTheLoais();

            cbxTheLoai.DataSource = theLoaiList;
            cbxTheLoai.DisplayMember = TheLoaiColumn.TenTheLoai;
            cbxTheLoai.ValueMember = TheLoaiColumn.IDTheLoai;

            //tac gia
            List<TacGiaDTO> tacGiaList = new List<TacGiaDTO>();
            TacGiaService tgService = new TacGiaService();
            cbxTacGia.DataSource = tgService.SelectAllTacGias();
            cbxTacGia.DisplayMember = TacGiaColumn.TenTacGia;
            cbxTacGia.ValueMember = TacGiaColumn.IDTacGia;

            //textbox
            tbxDonGia.Text = "0";
        }

        private void btnThem_Click(object sender, EventArgs e) {
            //kiem tra thong tin nhap vao
            if (tbxTenSach.Text.Trim() == "") {
                MessageBox.Show("Chưa nhập tên sách");
                tbxTenSach.Focus();
                return;
            }
            if (cbxTacGia.SelectedIndex == -1) {
                MessageBox.Show("Chưa chọn tác giả");
                cbxTacGia.Focus();
                return;
            }

            SachDTO sach = new SachDTO();
            sach.DonGiaSach = Convert.ToInt32(tbxDonGia.Text);
            sach.MaSach = tbxMaSach.Text;
            sach.SoLuongTon = 0;
            sach.TenSach = tbxTenSach.Text;
            sach.IDTacGia = (int) cbxTacGia.SelectedValue;
            sach.IDTheLoai = (int)cbxTheLoai.SelectedValue;

            SachService service = new SachService();
            if (service.AddNewBook(sach)) {
                MessageBox.Show("Thêm sách thành công");
                //frmSachMoi_Load(sender, e);
                tbxDonGia.Text = "0";
                tbxTenSach.Text = "";
                cbxTheLoai.SelectedIndex = 0;
            }
            else
                return;
        }

        private void tbxDonGia_TextChanged(object sender, EventArgs e) {
            if (tbxDonGia.Text == "")
                tbxDonGia.Text = "0";
        }

        private void button2_Click(object sender, EventArgs e) {
            Close();
        }

    }
}
